<%@page import="ContentManagers.RoleManagers.CustomerSessionManager"%>
<%@page import="ContentManagers.RoleManagers.AuthenticationManager"%>
<%@page import="ContentManagers.RoleManagers.PMManager"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="User Interface/css/user_int.css" />
        <link rel="stylesheet" type="text/css" href="User Interface/Javascript/js.js" />
    </head>
    <body>
         <% //employ temporary session management here
            try{
                boolean check = false;
        
                check = new AuthenticationManager().inSuchDB(1, request, response);
                
       if(check == false){%>
                <%@ include file="index_header.jsp"%>
                <%  }
                else
                { %>
                <!--script> alert('hello'); </script-->
                <%@ include file="user_header.jsp" %>
                <% }
            //Call USER header file. The header contains the links for other pages. %> 
            
       <div id ="body_wrapper">
            <% //Actual Content of the USER Purchases page starts here. %>
      
            <%@ include file="default_left_sidebar.jsp" %>
            
            <div id="searchResults" class="content_box">
            <% //process search here 
            
                try{
                    ResultSet rs=null;
                    //System.out.println(request.getParameter("byCategory"));
                    if(request.getParameter("byCategory")!=null)
                    {
                            rs = new PMManager().searchItem("", Validator.inputSanitizer(request.getParameter("byCategory").toString()));
                    }
                    else
                    {
                            rs = new PMManager().searchItem(Validator.inputSanitizer(request.getParameter("searchItems")), Validator.inputSanitizer(request.getParameter("searchType")));
                    }
                    if(rs == null || rs.first()==false){
                     %> 
                     No items matched your search.
                    <% }
                    else
                        {   
                            rs.beforeFirst();
                            while(rs.next())
                            {
                                %> 
                                
                                <div class="searchItem_result">
                                    <table>
                                        <tr>
                                            <td>
                                                <div class="searchItem_result_image">
                                                    <img src="User Interface/images/default_item_image.png">
                                                </div>
                                            </td>
                                            
                                            <td>
                                                <div class="searchItem_result_info">
                                                    <div class="addtocartForm">
                                                        <form name ="add_item_<%out.print(rs.getString("product_id"));%>" method="post" onsubmit="return addToCartCheck(this)" action="ServletAddToCart">
                                                            Qty:&nbsp;<input style="width:20px; height:13px;"type="text" value="--" name="quantity" maxlength="3" autocomplete="off" onfocus="clearInitial(this, '--')"/> 
                                                                <input class="invisible" type="text" name="productID" value="<% out.println(rs.getString("product_id")); %>"/>
                                                                <input type="submit" name="submitAdd" value="Add to Cart"/>
                                                        </form>
                                                    </div>
                                                     
                                                        <a href="viewItem.jsp?productID=<% out.println(rs.getString("product_id")); %> ">
                                                        <%  out.println(rs.getString("product_name")); %></a>
                                                        <div class="searchItem_result_info_stuff">
                                                        <%  
                                                            out.println("<br>" + rs.getString("description"));
                                                            out.println("<br>PhP &nbsp;" + rs.getString("price"));
                                                        %>
                                                        </div>
                                                    
                                                </div>
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                                
                                <%

                            }
                        }
                   }catch(Exception e){ }


        %>
            </div>
            
       </div>
        
       <%@ include file="user_footer.jsp" %>
       <% //Call USER footer file %>
    </body>
</html>


<% 
   }catch(Exception e)
        {   session = request.getSession();
            session.setAttribute("username", "xxxxdefaultxxxx");
            session.setAttribute("sessionID", "xxxxdefaultxxxx");
       
            response.sendRedirect("searchpage.jsp");
        }%>